package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.model.Result;
import com.spsoft.vatoutinv.model.dto.vmsmanualinvoice.VmsManualInvoiceDto;
import com.spsoft.vatoutinv.model.dto.vmsmanualinvoice.VmsManualPageDto;
import com.spsoft.vatoutinv.model.vo.vmsmanualinvoice.VmsManualBaseVo;
import com.spsoft.vatoutinv.model.vo.vmsmanualinvoice.VmsManualDefaultVo;
import com.spsoft.vatoutinv.model.vo.vmsmanualinvoice.VmsManualInvoiceVo;
import com.spsoft.vatoutinv.service.vmsmanualinvoice.VmsManualBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 手工发票开具 前端控制器
 * </p>
 *
 * @author chenxudong
 * @since 2023-12-15
 */
@RestController
@Api(tags = {"手工发票开具管理"})
@RequestMapping("/vmsManualInvoicing")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsManualInvoicingController extends BaseController {

    public static final String LOG_MSG = "(手工开票控制层)";

    private final VmsManualBaseService vmsManualBaseService;

    @ApiOperation("新增发票前的校验")
    @AnonymousAccess(value = "/checkBeforeAdd", method = RequestMethod.POST, permissionCode = "VMS_HANDINVOICEADD")
    @Log(value = "新增发票前的校验", type = LogActionType.SELECT, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public Result<Object> checkBeforeAdd() {
        log.info("{}新增发票前的校验", LOG_MSG);
        return vmsManualBaseService.checkBeforeAdd(getDeptCode());
    }

    @ApiOperation("获取默认带出数据")
    @AnonymousAccess(value = "/getBaseInfo", method = RequestMethod.POST, permissionCode = "VMS_FULL_MANUAL_SAVE")
    @Log(value = "获取默认带出数据", type = LogActionType.SELECT, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public Result<VmsManualDefaultVo> getBaseInfo() {
        log.info("{}获取默认带出数据", LOG_MSG);
        return vmsManualBaseService.getBaseInfo(getUser());
    }

    @ApiOperation("获取发票列表分页数据")
    @AnonymousAccess(value = "/getPageInfo", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "获取发票列表分页数据", type = LogActionType.SELECT, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public Result<IPage<VmsManualBaseVo>> getPageInfo(@RequestBody VmsManualPageDto vmsManualPageDto) {
        log.info("{}获取发票列表分页数据，入参：{}", LOG_MSG, JSONObject.toJSONString(vmsManualPageDto));
        return vmsManualBaseService.getPageInfo(vmsManualPageDto);
    }

    @ApiOperation("保存发票数据")
    @AnonymousAccess(value = "/checkAndSave", method = RequestMethod.POST, permissionCode = "VMS_FULL_MANUAL_SAVE")
    @Log(value = "单张发票校验并保存", type = LogActionType.ADD, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public Result<Object> singleInvoiceCheck(@RequestBody VmsManualInvoiceDto vmsManualInvoiceDto) {
        log.info("{}单张发票校验并保存，入参：{}", LOG_MSG, JSONObject.toJSONString(vmsManualInvoiceDto));
        return vmsManualBaseService.checkAndSave(vmsManualInvoiceDto, getUser());
    }

    @ApiOperation("获取单张发票详情")
    @AnonymousAccess(value = "/getInvoiceInfo", method = RequestMethod.POST, permissionCode = "VMS_HANDINVOICEADD")
    @Log(value = "获取单张发票详情", type = LogActionType.SELECT, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseId", value = "手工开票-发票ID", dataTypeClass = Long.class, dataType = "Long", paramType = "query", required = true)
    })
    public Result<VmsManualInvoiceVo> getInvoiceInfo(@RequestParam(value = "baseId") Long baseId) {
        log.info("{}获取单张发票详情，发票id：{}", LOG_MSG, baseId);
        return vmsManualBaseService.getInvoiceInfo(baseId);
    }

    @ApiOperation("修改发票信息")
    @AnonymousAccess(value = "/updateInvoice", method = RequestMethod.POST, permissionCode = "VMS_FULL_MANUAL_SAVE")
    @Log(value = "修改发票信息", type = LogActionType.UPDATE, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public Result<Object> updateInvoice(@RequestBody VmsManualInvoiceDto vmsManualInvoiceDto) {
        log.info("{}修改发票信息，入参：{}", LOG_MSG, JSONObject.toJSONString(vmsManualInvoiceDto));
        return vmsManualBaseService.updateInvoice(vmsManualInvoiceDto, getUser());
    }

    @ApiOperation("删除发票信息")
    @AnonymousAccess(value = "/deleteInvoice", method = RequestMethod.POST, permissionCode = "VMS_FULL_MANUAL_DELETE")
    @Log(value = "删除发票信息", type = LogActionType.DELETE, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseId", value = "手工开票-发票ID", dataTypeClass = Long.class, dataType = "Long", paramType = "query", required = true)
    })
    public Result<Object> deleteInvoice(@RequestParam(value = "baseId") Long baseId) {
        log.info("{}删除发票信息，发票id：{}", LOG_MSG, baseId);
        return vmsManualBaseService.deleteInvoice(baseId);
    }

    @ApiOperation("下载Excel导入模板")
    @AnonymousAccess(value = "/downloadExcel", method = RequestMethod.GET, permissionCode = "VMS_FULL_MANUAL_SAVE")
    @Log(value = "下载Excel导入模板", type = LogActionType.DOWNLOAD, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    public void downloadExcel(HttpServletResponse response) {
        log.info("{}下载Excel导入模板", LOG_MSG);
        vmsManualBaseService.downloadExcel(response);
    }

    @ApiOperation("读取Excel")
    @AnonymousAccess(value = "/readExcel", method = RequestMethod.POST, permissionCode = "VMS_FULL_MANUAL_SAVE")
    @Log(value = "读取Excel", type = LogActionType.UPLOAD, menuName = LogMenuEnum.ELECTRONICS_MANUAL_INVOICING)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象", dataTypeClass = MultipartFile.class, dataType = "MultipartFile", paramType = "query", required = true),
            @ApiImplicitParam(name = "fppz", value = "发票票种", dataTypeClass = String.class, dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "applyId", value = "审批流id", dataTypeClass = Long.class, dataType = "Long", paramType = "query", required = true)
    })
    public Result<Object> readExcel(@RequestParam(value = "file") MultipartFile file,
                                    @RequestParam(value = "fppz") String fppz,
                                    @RequestParam(value = "applyId") Long applyId) {
        log.info("{}读取并保存Excel", LOG_MSG);
        return vmsManualBaseService.readAndSaveExcel(file, fppz, applyId, getUser());
    }

}

